#!/bin/env python
# coding: utf-8

from __future__ import print_function

import cv2
import sys
import os

 
# 修改灰色为白色
def gray2white(img):
  sp=img.shape # 获取图片维度
  width=sp[0] # 宽度
  height=sp[1] # 高度
  if len(img[0,0]) == 4:
    white_pixel = [255,255,255,255]
  else:
    white_pixel = [255,255,255]
  for yh in range(height):
    for xw in range(width):
      color_d=img[xw,yh] # 遍历图像每一个点，获取到每个点的颜色数据
      #print(color_d)
      # 灰色值
      if color_d[0] > 140 and color_d[1] > 140 and color_d[2] > 140:
        img[xw,yh] = white_pixel
        
  return img
 
 
if __name__ == "__main__":
  if len(sys.argv) < 2:
    print("Usage: {} infile [outfile]".format(sys.argv[0]))
    sys.exit(0)
    
  infile = sys.argv[1]
  if len(sys.argv) > 2:
    outfile = sys.argv[2]
  else:
    fname = os.path.basename(infile)
    outfile = os.path.join(os.path.dirname(infile), "w_{}".format(fname))
    
  img=cv2.imread(infile,-1)
  img=gray2white(img)
  cv2.imwrite(outfile,img)

